#include <stdlib.h>
#include <string.h>
#include "pkcs11.h"
#include "session.h"

int	MSG_RECV_BUFFER_SIZE(CK_SESSION_HANDLE session)
{
	session_t *s = (session_t *)session;
	return s->bf->size;
}

uint8_t *MSG_SEND_BUFFER_START(CK_SESSION_HANDLE session)
{
	session_t *s = (session_t *)session;
	return  s->bf->ptr;
}

uint8_t *MSG_RECV_BUFFER_START(CK_SESSION_HANDLE session)
{
	session_t *s = (session_t *)session;
	return  s->bf->ptr;
}

int	session_send(CK_SESSION_HANDLE session, uint8_t *data, int len)
{
	session_t *s = (session_t *)session;
	return chan_send(s->chan, data, len);
}

int	session_recv(CK_SESSION_HANDLE session, uint8_t *buf, int len)
{
	session_t *s = (session_t *)session;
	return chan_recv(s->chan, buf, len);
}

CK_RV C_OpenSession
(
	CK_SLOT_ID            slotID,        
	CK_FLAGS              flags,         
	CK_VOID_PTR           pApplication, 
	CK_NOTIFY             Notify,   
	CK_SESSION_HANDLE_PTR phSession 
)
{
	session_t *s;

	s = (session_t *)malloc(sizeof(session_t));
	memset(s, 0, sizeof(session_t));
	s->chan = chan_open("127.0.0.1", 3344);
	s->bf = (buffer_t *)malloc(sizeof(buffer_t));
	s->bf->ptr = malloc(MAX_SESSION_BUF);
	s->bf->size = MAX_SESSION_BUF;
	s->bf->len = 0;
	(*phSession) = (CK_SESSION_HANDLE)s;
	return 0;
}
